//
//  FSCLeftViewController.swift
//  FamiliesSchoolConnection
//
//  Created by 花花蔡 on 2016/12/23.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

import UIKit
import SnapKit

class FSCLeftViewController: UIViewController {

    @IBOutlet weak var blueBgView: UIView!
    //头像
    @IBOutlet var leftIconView: UIImageView!
    //昵称
    @IBOutlet var leftTitleL: UILabel!
    //账号信息
    @IBOutlet var leftAccountView: UILabel!
    //班级
    @IBOutlet var leftClassL: UILabel!
    //表现分
    @IBOutlet var leftScoreL: UILabel!
    //学校
    @IBOutlet var lelftSchoolL: UILabel!
    //背景
    @IBOutlet var leftscoreBgV: UIView!
    //按钮视图
    var leftView = FSCLeftView()
    
        
    
//MARK:视图生命周期

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        leftView.addLeftView(VC:self)
        btnClicked()
        
    }

    
    func setupUI() -> Void {
        
        //赋值
        let studentName = userInfo["studentName"] as? String
        leftTitleL.text = (studentName ?? "") + " 家长"
        leftAccountView.text = userInfo["studentNum"] as? String
        leftClassL.text = userInfo["classname"] as? String
        leftScoreL.text = userInfo["studentScore"] as? String
        lelftSchoolL.text = userInfo["schoolname"] as? String
        let score = Int(leftScoreL.text ?? "") ?? 0
        //获得图片的加密字符串
        if  let base64Str = userInfo["studentphoto"] as? String,
            let imgData = Data(base64Encoded: base64Str, options: .ignoreUnknownCharacters)
        {
            leftIconView.image = UIImage(data: imgData)
        }
       
        //将加密字符串转换为二进制
        //设置头像圆角
        leftIconView.layer.cornerRadius = 40
        leftIconView.layer.masksToBounds = true
        //设置边框
        leftIconView.layer.borderWidth = 3
        leftIconView.layer.borderColor = UIColor(colorLiteralRed: 1.0, green: 1.0, blue: 1.0, alpha: 0.6).cgColor
        //设置阴影
        let iconLayer = CALayer()
        iconLayer.position = leftIconView.layer.position
        iconLayer.bounds = leftIconView.layer.bounds
        iconLayer.cornerRadius = leftIconView.layer.cornerRadius
        iconLayer.backgroundColor = UIColor.gray.cgColor
        iconLayer.shadowColor = UIColor.black.cgColor
        iconLayer.shadowRadius = 5
        iconLayer.shadowOffset = CGSize(width: 3.0, height: 3.0)
        iconLayer.shadowOpacity = 1
        blueBgView.layer.addSublayer(iconLayer)
        blueBgView.bringSubview(toFront: leftIconView)
        leftscoreBgV.backgroundColor = UIColor.clear
        
    
        //表现分等级，用于点亮成长树
        var scoreRank = 0
        switch score {
        case 0..<10 :
            scoreRank = 0
        case 10..<50 :
            scoreRank = 1
        case 50 ..< 150 :
            scoreRank = 2
        case 150..<350 :
            scoreRank = 3
        case 350..<850 :
            scoreRank = 4
        default:
            scoreRank = 5
        }
        
        //循环添加图片
        for item in 1...5 {
            
            var imageName = ""
            if scoreRank >= item {
                imageName = "jujube\(item)_light"
            }else {
                imageName = "jujube\(item)_dark"
            }
            //初始化图片视图
            let imageView = UIImageView(image: UIImage(named: imageName))
            //添加到背景视图上面
            leftscoreBgV.addSubview(imageView)
            
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        //获得背景视图的宽度:通过storyBoard加载的视图，在viewDidLoad中得不得视图正确的宽度，需要在viewDidAppear中才能得到
        //获得背景视图的高度
        let magin = leftscoreBgV.frame.width / 5
        
        for (i,v) in leftscoreBgV.subviews.enumerated() {
            
            let offsetM = magin * CGFloat(i) + magin/2
            
            v.snp.makeConstraints({ (make) in
                make.centerX.equalTo(leftscoreBgV.snp.left).offset(offsetM)
                make.centerY.equalTo(leftscoreBgV.snp.bottom).offset(-v.frame.height/2)
            })
        }
        /*
                let bottomY = leftscoreBgV.frame.height
                //获得上面的图片
                for (i,v) in leftscoreBgV.subviews.enumerated() {
        
                    v.center = CGPoint(x: magin/2 + CGFloat(i)*magin, y: bottomY - v.frame.height/2)
                }
        */
        
    }
    
//MARK:btn的回调事件
    func btnClicked() -> Void {
        
        leftView.scheduleBtnBlock = { [weak self] in
            //课程表
            self?.present(FSCClassViewController(), animated: true, completion: nil)
        }
        
        leftView.aboutUsBtnBlock = { [weak self] in
            //这里推出关于我们界面
            self?.present(FSCAboutUsController(), animated: true, completion: nil)
        }
        
        leftView.feedBackBtnBlock = { [weak self] in
            //意见反馈
            self?.present(FSCFeedBackViewController(), animated: true, completion: nil)
        }
        
        leftView.setUpBtnBlock = {[weak self] in
            self?.present(FSCSetupController(), animated: true, completion: nil)
        }
        
        leftView.exitBtnBlock = {
            //推出
            UIApplication.shared.keyWindow?.rootViewController = FSCLoginController()
            UserDefaults.standard.set(false, forKey: isLoginKey)
        }
    
    }


}
